package com.partner.frame.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import sun.nio.cs.ext.MacThai;

import com.partner.bean.member.Member;
import com.partner.bean.order.Order;
import com.partner.bean.order.Ordermi;
import com.partner.frame.support.util.DateConvertUtil;
import com.partner.frame.util.MemberUtil;
import com.partner.service.member.MemberService;
import com.partner.service.order.OrderService;
import com.partner.service.order.OrdermiService;
import com.partner.service.sysbase.SystemSettingService;

/** 
 * 数据处理计算公司分红
 * @author dwr
 * @date 2018-09-04 
 */  
@Component 
public class WorkFhTask {
	
	@Autowired
	private SystemSettingService systemSettingService ; 
	@Autowired
	private OrderService orderService;
	@Autowired
	private MemberService memberService;
	boolean isxh = true ; 
	//private final static Logger log = LoggerFactory.getLogger(WorkTask.class);
	
	/**  
     *  每月1号凌晨1点计算公司分红
     * @throws ParseException 
     */    
	@Scheduled(cron = "0 0 1 * * ?")
	//@Scheduled(cron = "0 0 1 1 * ? *")
	public void downLoadBizUserMacData() throws Exception {
	    
    	//System.out.println("定时器数据处理.................");
		//处理分红机制(总监与名誉总监)
		//查询公司总收益
		Order order1 = new Order();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date sdate = DateConvertUtil.getDateBefore(new Date(), 1);
		order1.setStarttime(sdf.format(sdate));//间隔开始时间
		order1.setEndtime(sdf.format(sdate));//间隔结束时间
		Integer allorder = orderService.report1(order1);//收入总和
		if(allorder>0){
			Integer[] level1 = {2,3};
			List<Member> memberList1 = memberService.selectLevelMember(level1);
			double fjf1 = allorder*0.08 / memberList1.size(); //8%平分给所有的总监与名誉总监
			Integer fjfi1 =  (int)fjf1 ; //Integer.valueOf(Math.floor(fjf1)+"");
			for(int i=0 ;i<memberList1.size() ; i++ ){
				Member oldmember1 = memberList1.get(i);
				Member upmember1 = new Member();
				upmember1.setId(oldmember1.getId());
				upmember1.setSaleamount(oldmember1.getSaleamount()+fjfi1);
				memberService.updateByPrimaryKey(upmember1);
				MemberUtil.insertOrder(null, "分红", fjfi1, 5, "分红", oldmember1, orderService);
			}
			
			//处理分红机制(盟主人数+名誉盟主)
			Integer[] level2 = {4,5};
			List<Member> memberList2 = memberService.selectLevelMember(level2);
			//查询公司总收益
			double fjf2 = allorder*0.05/memberList2.size() ; //8%平分给所有的盟主人数+名誉盟主
			Integer fjfi2 = (int)fjf2;
			for(int i=0 ;i<memberList2.size() ; i++ ){
				Member oldmember2 = memberList2.get(i);
				Member upmember2 = new Member();
				upmember2.setId(oldmember2.getId());
				upmember2.setSaleamount(oldmember2.getSaleamount()+fjfi2);
				memberService.updateByPrimaryKey(upmember2);
				MemberUtil.insertOrder(null, "分红", fjfi2, 5, "分红", oldmember2, orderService);
			}
			
			//处理分红机制(董事人数+名誉董事)
			Integer[] level3 = {6,7};
			List<Member> memberList3 = memberService.selectLevelMember(level3);
			//查询公司总收益
			double fjf3 = allorder*0.04/memberList3.size() ; //8%平分给所有的董事人数+名誉董事
			Integer fjfi3 = (int)fjf3;
			for(int i=0 ;i<memberList3.size() ; i++ ){
				Member oldmember3 = memberList3.get(i);
				Member upmember3 = new Member();
				upmember3.setId(oldmember3.getId());
				upmember3.setSaleamount(oldmember3.getSaleamount()+fjfi3);
				memberService.updateByPrimaryKey(upmember3);
				MemberUtil.insertOrder(null, "分红", fjfi3, 5, "分红", oldmember3, orderService);
			}
		}
    }

}
